package com.zhiyou100.controller.user;

import com.zhiyou100.dao.Impl.UserDaoImpl;
import com.zhiyou100.emtity.User;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.BeanUtilsBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

/**
 * @description:
 * @author: 马跃
 * @create: 2021-01-20 10:58
 **/
@WebServlet("/userAdd")
public class UserAddServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;;charset=utf-8");
        User user = new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        if (request.getParameter("confirmPassword").equals(user.getU_password())){
            new UserDaoImpl().add(user);
            request.getRequestDispatcher("/userQueryAll").forward(request, response);
        }else {
            response.getWriter().print("<font style='color:red;font-size:40'>俩次密码不一致</font>");
            response.getWriter().print("<br><a href='/Adminastrator/user/addUser.html'>从新添加</a>");
            response.getWriter().print("<br><a href='/userQueryAll'>返回主页</a>");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
